<?php

//获取值信息
function Customer($mid, $id, $field) {

	if ($mid or $id or $field) {
		$map['id'] = $id;
		return M(GetModelTable($mid))->where($map)->cache(true)->getField($field);

	}
}
//跟据用户id获取用户信息
function getUserInfos($id, $field) {
	if (!$id) {
		return false;
	}
	if ($field) {
		return M('user')->where(array('id' => $id))->cache(true)->getField($field);
	} else {
		return M('user')->where(array('id' => $id))->cache(true)->find();
	}

}

//导出excel
function impcsv($field, $list) {
	$csv = new Think\Csv();
	$csv->put_csv($list, $field);
}
function _cny_map_unit($list, $units) {
	$ul = count($units);
	$xs = array();
	foreach (array_reverse($list) as $x) {
		$l = count($xs);
		if ($x != "0" || !($l % 4)) {
			$n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
		} else {
			$n = is_numeric($xs[0][0]) ? $x : '';
		}

		array_unshift($xs, $n);
	}
	return $xs;
}
//金额数字转中文大写
function cny($ns) {
	if (!$ns) {
		return false;
	}
	static $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"),
	$cnyunits = array("圆", "角", "分"),
	$grees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿");
	list($ns1, $ns2) = explode(".", $ns, 2);
	$ns2 = array_filter(array($ns2[1], $ns2[0]));
	$ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), ""));
	$ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunits)));
	return str_replace(array_keys($cnums), $cnums, $ret);
}
//为栏目加子级样分隔线
function navListColumn($list) {
	$options = get_tree_option($list['list'], 0);
	for ($i = 0; $i < count($options); $i++) {
		$options[$i]['lanmumingcheng'] = str_repeat("-", $options[$i]['depth'] * 4) . $options[$i]['lanmumingcheng'];
	}
	$list['list'] = $options;
	return $list;
}

//客户列表增加业务员姓名
function customerlist($list) {
	for ($i = 0; $i < count($list['list']); $i++) {
		$list['list'][$i]['user'] = getUserInfos($list['list'][$i]['uid'], 'username');
	}
	$list['head'][] = array('field' => 'user', 'title' => '业务员', 'type' => 'text|varchar');
	return $list;
}
//获取用户组权限
function GetuserGroupAccess() {
	return M('auth_group_access')->where(array('uid' => getuserid()))->getField('group_id');

}
//公告消息增加设置状态
function state($list) {

	for ($i = 0; $i < count($list['list']); $i++) {
		$browstuid = $list['list'][$i]['browstuid'];
		if (!$browstuid) {
			$list['list'][$i]['st'] = "<a href='" . U('Index/state', array('mid' => 32, 'id' => $list['list'][$i]['id'])) . "' class='getajax btn btn-default'>设置已阅</a>";
		} else {
			if (in_array(getuserid(), json_decode($browstuid))) {
				$list['list'][$i]['st'] = "<a href='#' class='btn btn-default' disabled='disabled'>已阅</a>";
			} else {
				$list['list'][$i]['st'] = "<a href='" . U('Index/state', array('mid' => 32, 'id' => $list['list'][$i]['id'])) . "' class='getajax btn btn-default'>设置已阅</a>";
			}

		}
		$list['list'][$i]['user'] = getUserInfos($list['list'][$i]['uid'], 'username');

	}

	$list['head'][] = array('field' => 'st', 'title' => '阅读状态', 'type' => 'text|varchar');
	$list['head'][] = array('field' => 'user', 'title' => '发布人', 'type' => 'text|varchar');

	return $list;
}
function get_tree_child($data, $fid) {
	$result = array();
	$fids = array($fid);
	do {
		$cids = array();
		$flag = false;
		foreach ($fids as $fid) {
			for ($i = count($data) - 1; $i >= 0; $i--) {
				$node = $data[$i];
				if ($node['pid'] == $fid) {
					array_splice($data, $i, 1);
					$result[] = $node['id'];
					$cids[] = $node['id'];
					$flag = true;
				}
			}
		}
		$fids = $cids;
	} while ($flag === true);
	return $result;
}

function get_tree_parent($data, $id) {
	$result = array();
	$obj = array();
	foreach ($data as $node) {
		$obj[$node['id']] = $node;
	}

	$value = isset($obj[$id]) ? $obj[$id] : null;
	while ($value) {
		$id = null;
		foreach ($data as $node) {
			if ($node['id'] == $value['pid']) {
				$id = $node['id'];
				$result[] = $node['id'];
				break;
			}
		}
		if ($id === null) {
			$result[] = $value['pid'];
		}
		$value = isset($obj[$id]) ? $obj[$id] : null;
	}
	unset($obj);
	return $result;
}

function get_tree_ul($data, $fid) {
	$stack = array($fid);
	$child = array();
	$added_left = array();
	$added_right = array();
	$html_left = array();
	$html_right = array();
	$obj = array();
	$loop = 0;
	foreach ($data as $node) {
		$pid = $node['pid'];
		if (!isset($child[$pid])) {
			$child[$pid] = array();
		}
		array_push($child[$pid], $node['id']);
		$obj[$node['id']] = $node;
	}

	while (count($stack) > 0) {
		$id = $stack[0];
		$flag = false;
		$node = isset($obj[$id]) ? $obj[$id] : null;
		if (isset($child[$id])) {
			$cids = $child[$id];
			$length = count($cids);
			for ($i = $length - 1; $i >= 0; $i--) {
				array_unshift($stack, $cids[$i]);
			}
			$obj[$cids[$length - 1]]['isLastChild'] = true;
			$obj[$cids[0]]['isFirstChild'] = true;
			$flag = true;
		}
		if ($id != $fid && $node && !isset($added_left[$id])) {
			if (isset($node['isFirstChild']) && isset($node['isLastChild'])) {
				$html_left[] = '<li class="first-child last-child">';
			} else if (isset($node['isFirstChild'])) {
				$html_left[] = '<li class="first-child">';
			} else if (isset($node['isLastChild'])) {
				$html_left[] = '<li class="last-child">';
			} else {
				$html_left[] = '<li>';
			}
			$html_left[] = ($flag === true) ? "<div>{$node['title']}</div><ul>" : "<div>{$node['title']}</div>";
			$added_left[$id] = true;
		}
		if ($id != $fid && $node && !isset($added_right[$id])) {
			$html_right[] = ($flag === true) ? '</ul></li>' : '</li>';
			$added_right[$id] = true;
		}

		if ($flag == false) {
			if ($node) {
				$cids = $child[$node['pid']];
				for ($i = count($cids) - 1; $i >= 0; $i--) {
					if ($cids[$i] == $id) {
						array_splice($child[$node['fid']], $i, 1);
						break;
					}
				}
				if (count($child[$node['pid']]) == 0) {
					$child[$node['pid']] = null;
				}
			}
			array_push($html_left, array_pop($html_right));
			array_shift($stack);
		}
		$loop++;
		if ($loop > 5000) {
			return $html_left;
		}

	}
	unset($child);
	unset($obj);
	return implode('', $html_left);
}

function get_tree_option($data, $fid) {
	$stack = array($fid);
	$child = array();
	$added = array();
	$options = array();
	$obj = array();
	$loop = 0;
	$depth = -1;
	foreach ($data as $node) {
		$pid = $node['pid'];
		if (!isset($child[$pid])) {
			$child[$pid] = array();
		}
		array_push($child[$pid], $node['id']);
		$obj[$node['id']] = $node;
	}

	while (count($stack) > 0) {
		$id = $stack[0];
		$flag = false;
		$node = isset($obj[$id]) ? $obj[$id] : null;
		if (isset($child[$id])) {
			for ($i = count($child[$id]) - 1; $i >= 0; $i--) {
				array_unshift($stack, $child[$id][$i]);
			}
			$flag = true;
		}
		if ($id != $fid && $node && !isset($added[$id])) {
			$node['depth'] = $depth;
			$options[] = $node;
			$added[$id] = true;
		}
		if ($flag == true) {
			$depth++;
		} else {
			if ($node) {
				for ($i = count($child[$node['pid']]) - 1; $i >= 0; $i--) {
					if ($child[$node['pid']][$i] == $id) {
						array_splice($child[$node['pid']], $i, 1);
						break;
					}
				}
				if (count($child[$node['pid']]) == 0) {
					$child[$node['pid']] = null;
					$depth--;
				}
			}
			array_shift($stack);
		}
		$loop++;
		if ($loop > 5000) {
			return $options;
		}

	}
	unset($child);
	unset($obj);
	return $options;
}